package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 12/3/20 9:42 AM
 *
 * https://leetcode-cn.com/problems/maximum-binary-tree/
 */
public class _654_最大二叉树_01 {


    public TreeNode constructMaximumBinaryTree(int[] nums) {

        if (nums == null || nums.length == 0) {

            return null;
        }


        return findRoot(nums,0,nums.length);
    }

    private TreeNode findRoot(int[] nums, int start, int end) {

        if (end - start == 1) {

            return new TreeNode(nums[start]);
        }

        int index = findMaxIndex(nums, start, end);

        TreeNode root = new TreeNode(nums[index]);

        if (index > start) {

            root.left = findRoot(nums,start,index);
        }

        if (index +1 < end) {

            root.right = findRoot(nums,index + 1, end);
        }

        return root;
    }

    private int findMaxIndex(int[] nums, int start, int end) {

        int index = start;
        for (int i = start + 1; i < end; i++) {

            if (nums[i] > nums[index]) {

                index = i;
            }
        }

        return index;
    }


    public static void main(String[] args) {
        int a = 100;

        // 查看第3位是1还是0
        int flag = (a & (1 << 2));
        System.out.println(flag);

        // 设置第3位的值设置为0
        a = (a & ~(1<<2));
        System.out.println(a);

        // 借助 bc
        // echo "obase=2;100" | bc
        // echo "ibase=2;1100000" | bc
        // 把第2位设置为1
        a = (a | (1<<1));
        System.out.println(a);
    }


}
